package com.xinke.module.system.service.sms;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.xinke.module.system.model.vo.sms.*;
import com.xinke.module.system.model.entity.sms.SmsCodeDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.sms.SmsCodeMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;
import static com.xinke.module.system.enums.ErrorCodeConstants.*;

/**
 * 手机验证码 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class SmsCodeServiceImpl implements SmsCodeService {

    @Resource
    private SmsCodeMapper smsCodeMapper;

    @Override
    public Long createSmsCode(SmsCodeSaveReqVO createReqVO) {
        // 插入
        SmsCodeDO smsCode = BeanUtils.toBean(createReqVO, SmsCodeDO.class);
        smsCodeMapper.insert(smsCode);
        // 返回
        return smsCode.getSmsId();
    }

    @Override
    public void updateSmsCode(SmsCodeSaveReqVO updateReqVO) {
        // 校验存在
        validateSmsCodeExists(updateReqVO.getSmsId());
        // 更新
        SmsCodeDO updateObj = BeanUtils.toBean(updateReqVO, SmsCodeDO.class);
        smsCodeMapper.updateById(updateObj);
    }

    @Override
    public void deleteSmsCode(Long smsId) {
        // 校验存在
        validateSmsCodeExists(smsId);
        // 删除
        smsCodeMapper.deleteById(smsId);
    }

    private void validateSmsCodeExists(Long smsId) {
        if (smsCodeMapper.selectById(smsId) == null) {
            throw exception(null);
        }
    }

    @Override
    public SmsCodeDO getSmsCode(Long smsId) {
        return smsCodeMapper.selectById(smsId);
    }

    @Override
    public PageResult<SmsCodeDO> getSmsCodePage(SmsCodePageReqVO pageReqVO) {
        return smsCodeMapper.selectPage(pageReqVO);
    }

}